package com.uflower.product.listener;

import com.alibaba.fastjson.TypeReference;
import com.rabbitmq.client.Channel;
import com.uflower.common.to.mq.OrderEntityTo;
import com.uflower.common.to.mq.StockLockedTo;
import com.uflower.common.utils.R;
import com.uflower.product.service.SkuInfoService;
import com.uflower.product.vo.OrderVo;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@RabbitListener(queues = "stock.release.stock.queue")
@Service
public class StockReleaseListener {
    @Autowired
    SkuInfoService  skuInfoService;
    @RabbitHandler
    public void handleStockLockRelease(StockLockedTo to, Message message, Channel channel) throws IOException {
        System.out.println("收到解锁库存的消息");
        try{
            skuInfoService.unlockStock(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
    //缺少库存工作单表，无法防止重复解锁库存
//    @RabbitHandler
//    public void handleOrderCloseRelease(OrderEntityTo orderEntityTo, Message message, Channel channel) throws IOException {
//        System.out.println("收到订单取消的消息,开始库存检索");
//        try{
//            skuInfoService.unlockStock(orderEntityTo);
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//        }catch (Exception e){
//            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
//        }
//    }
}
